package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Work;
import cn.wolfcode.crm.service.IWorkService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("work")
public class WorkController {
    @Autowired
    private IWorkService workService;

    @RequiresPermissions("work:index")
    @PermissionName("工作首页")
    @RequestMapping("index")
    public String index() {
        return "work/list";
    }





    @RequiresPermissions("work:save")
    @PermissionName("工作保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Work work){
        JsonResult result = new JsonResult();
        try {
            workService.insert(work);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存工作失败");
        }
        return result;
    }

    @RequiresPermissions("work:update")
    @PermissionName("工作编辑")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Work work){
        JsonResult result = new JsonResult();
        try {
            workService.updateByPrimaryKey(work);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("更新工作失败");
        }
        return result;
    }


    @RequestMapping("queryByEmpId")
    @ResponseBody
    public List<Work> queryByEmpId(Long id){
        return workService.queryByEmpId(id);
    }


}
